<?
require '../../common/init.php';
require 'cart_utility.php';
require 'history_utility.php';
require VIEW.'view_utility.php';

// language file 语言文件
include '../lang/'.$lang.'.php';

$vice_menu=new DB(Tvice_menu, $conn);
$vice_menu_category=new DB(Tvice_menu_category, $conn);

// 当前购物车内商品数量 cart qty 
$cart=get_cart();
$cart_qty=get_cart_qty();
$cart_order_price=get_order_price($currency_rate);

// customer login status 客户登录状态
$customer_id=$customer_grade=0;
$customer_nickname='Guest';
if(isset($_COOKIE['customer_id'])) $customer_id=encrypt($_COOKIE['customer_id'],'decode');
if(isset($_COOKIE['customer_nickname'])) $customer_nickname=encrypt($_COOKIE['customer_nickname'],'decode');

// 获取客户级别
$tem=$grade->get_rows(32,'','','start_score','asc');
$customer_grade=1;
$arr_grade=array();
foreach($tem as $val){ $arr_grade[$val['level']]=$val; }
// 客户资料$i
$i=array();
if($customer_id>0){
	$i=$customer->get_row($customer_id);
	$customer_grade=$i['level'];
}

// 获取正在进行的折扣
$where=array();
$where[]=array('name'=>'start_time','val'=>date('Y-m-d H:i:s'),'opn'=>'<');
$where[]=array('name'=>'end_time','val'=>date('Y-m-d H:i:s'),'opn'=>'>');
$tem=$sales_promotion->get_rows_advanced(32,$where);
$arr_event=array();
foreach($tem as $val) { $arr_event[]=$val['id']; }

// 获取购物车中产品的详细资料 get cart items
$tem_pro=array();
foreach ($cart as $row) $tem_pro[]=$row['pid'];
$cart_pro=array();
$where=array();
$where[]=array('name'=>'id','val'=>$tem_pro,'opn'=>'in');
$tem=$product->get_rows_advanced(32,$where);
foreach($tem as $row) $cart_pro[$row['id']]=$row;

// get all variation option value 产品多属性
$product_option_obj=$product_option->get_rows(0,'','','id','asc');
$option_label=array();
foreach($product_option_obj as $row){
		$option_label[$row['id']]=$row['name_'.$lang];
}

$product_option_value_obj=$product_option_value->get_rows(0,'','','id','asc');
$option_value=array();
foreach($product_option_value_obj as $row){
		$option_value[$row['id']]=$row['name_'.$lang];
}

// 获取所有分类 get all categories
$tree_product_category = $product_category->get_rows(1024,'','','seq_no');
$arr_product_category=array();
foreach ($tree_product_category as $val){
	$arr_product_category[$val['id']]=$val;
}

// 顶级分类 top level
$cate1=get_level_children($tree_product_category,0);
$str_cate=''; $str_cate_nav=''; $cate_index='';
$k=0;
foreach ($cate1 as $key1 => $val1){
	$odd='';
	if($k%2) $odd=' class="odd" ';
	$str_cate.='<li '.$odd.'><h3><a href="products-'.$val1['id'].'.html">'.$val1['name_'.$lang].'</a></h3>';
	$str_cate_nav.='<li '.$odd.'><h3><a href="products-'.$val1['id'].'.html">'.$val1['name_'.$lang].'</a></h3>';
	$cate2=get_level_children($tree_product_category,$val1['id']);
	$m=0;
	foreach($cate2 as $val2){
		$str_cate.='<a href="products-'.$val2['id'].'.html">'.$val2['name_'.$lang].'</a>';
		if($m<$config['MAX_SUB_CATEGORY_SHOWN']) $str_cate_nav.='<a href="products-'.$val2['id'].'.html">'.$val2['name_'.$lang].'</a>';
		$m++;
	}
	$str_cate.='</li>';
	$str_cate_nav.='</li>';
	if($k<$config['MAX_TOP_CATEGORY_SHOWN']) { $cate_index=$str_cate_nav; }
	$k++;
}

$cate_index.='<li '.$odd.'><h3><a href="category.html">'.WEB_VIEW_MORE.'...</a></h3>';


// 底部导航 vice menu
$tem=$vice_menu_category->get_rows(32,'','','seq_no');
$arr_vice_menu=array();
foreach($tem as $val){ $arr_vice_menu[$val['id']]=$val; }
$tem=$vice_menu->get_rows(1024,'language_id',$lang_id,'seq_no');
foreach($tem as $val){
	$arr_vice_menu[$val['category_id']]['menu'][]=$val;
}
//pre($arr_vice_menu); exit;


// 扫描取消过期未付款订单 scan invalid orders
// 最早时间
$fromtime = mktime(date('H')-ORDER_VALID_PERIOD,0,0,date("m"),date("d"),date("Y"));
$fromdate = date('Y-m-d H:i:s',$fromtime);
$where=array();
//$where[]=array('name'=>'customer_id','val'=>$customer_id);
$where[]=array('name'=>'order_status','val'=>2, 'opn'=>'<');
$where[]=array('name'=>'payment_method_id','val'=>5, 'opn'=>'!=');
$where[]=array('name'=>'created_time','val'=>$fromdate, 'opn'=>'<');
$tem=$sales_order->get_rows_advanced(10240,$where);
$in_array=array();
$in_array_order=array();
foreach($tem as $val){ $in_array[]=$val['id'];
$in_array_order[]=$val['ref_no'];}
$arr=array('order_status'=>'8'); // 系统取消状态
try {
	$db->begin_transaction();
	$sales_order->update($arr,'id',$in_array);
	// 恢复库存
	foreach ($in_array_order as $val){
		$product->restore_stock_by_order($val);
	}
	$db->commit();
}
catch(PDOException $ex){
	//Something went wrong rollback!
	$db->rollBack();
	pre($ex->getMessage());
} 

// hot keywords 热门关键词
$tem = explode(',',trim($config['SEARCH_KEYWORDS_'.LANG]));
$arr_keyword=array(); $k=0;
foreach($tem as $row) { $arr_keyword[]=trim($row); $k++; if($k==MAX_KEYWORD_SHOWN) break; }

// footer ad 页脚广告
$tem=$ad->get_rows(1,'category_id',3);
$ad_footer=$tem[0];

// end of file